package com.minimalist.common.config.trace;

import cn.hutool.core.util.IdUtil;
import com.minimalist.common.utils.CommonConstant;
import org.slf4j.MDC;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 应用启动时设置 traceId 的监听器
 *
 * @author minimalist
 */
@Component
public class ApplicationStartupTraceIdListener implements ApplicationListener<ApplicationStartedEvent>{

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event){
        // 为应用启动设置一个固定的 traceId
        String traceId = "startup-" + IdUtil.randomUUID();
        MDC.put(CommonConstant.TRACE_ID, traceId);
    }
}